/*created by hp
*on 18-10-14 下午2:14
*/
package com.happts.Utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.happts.Entity.RECORD;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CreateExcel {

    public void saveIn(RECORD record) throws IOException, WriteException {
        Random r = new Random();
        File f = new File("src/SaveInTicket/" + record.getCite() + "_" + record.getNumber()+"_"+r.nextInt()+".xls");
        WritableWorkbook workbook = Workbook.createWorkbook(f);
        WritableSheet sheet = workbook.createSheet("1", 0);
        Label lab = null;
        lab = new Label(0, 0, "菜籽油存单");
        sheet.addCell(lab);
        lab = new Label(0, 1, "姓名");
        sheet.addCell(lab);
        lab = new Label(1, 1, record.getName());
        sheet.addCell(lab);
        lab = new Label(3, 1, "地址");
        sheet.addCell(lab);
        lab = new Label(4, 1, record.getAddress());
        sheet.addCell(lab);
        lab = new Label(0, 2, "代收点");
        sheet.addCell(lab);
        lab = new Label(1, 2, record.getCite());
        sheet.addCell(lab);
        lab = new Label(3, 2, "单号");
        sheet.addCell(lab);
        lab = new Label(4, 2, record.getNumber());
        sheet.addCell(lab);
        lab = new Label(0, 4, "菜籽重量");
        sheet.addCell(lab);
        lab = new Label(1, 4, String.valueOf(record.getWeight()));
        sheet.addCell(lab);
        lab = new Label(0, 5, "菜籽油调率");
        sheet.addCell(lab);
        lab = new Label(1, 5, String.valueOf(record.getVegRate()));
        sheet.addCell(lab);
        lab = new Label(3, 5, "菜籽油重量");
        sheet.addCell(lab);
        lab = new Label(4, 5, new java.text.DecimalFormat("#.00").format(record.getLeftVeg()));
        sheet.addCell(lab);
        lab = new Label(0, 6, "精制油调率");
        sheet.addCell(lab);
        lab = new Label(1, 6, String.valueOf(record.getRefinedRate()));
        sheet.addCell(lab);
        lab = new Label(3, 6, "精制油重量");
        sheet.addCell(lab);
        lab = new Label(4, 6, new java.text.DecimalFormat("#.00").format(record.getLeftRefined()));
        sheet.addCell(lab);
        workbook.write();
        workbook.close();
    }

    public void fetchOil(RECORD record, boolean isVeg, double Weight) throws WriteException, IOException {
        Random r = new Random();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        File f = new File("src/FetchOilTicket/"+record.getCite()+"_"+df.format(new Date())+"_"+record.getNumber()+"_"+r.nextInt()%100+".xls");
        WritableWorkbook workbook = Workbook.createWorkbook(f);
        WritableSheet sheet = workbook.createSheet("1", 0);
        Label lab = null;
        lab = new Label(0, 0, "菜籽取油单");
        sheet.addCell(lab);
        lab = new Label(0, 1, "日期");
        sheet.addCell(lab);
        lab = new Label(1, 1, df.format(new Date()));
        sheet.addCell(lab);
        lab = new Label(0, 2, "姓名");
        sheet.addCell(lab);
        lab = new Label(1, 2, record.getName());
        sheet.addCell(lab);
        lab = new Label(3, 2, "地址");
        sheet.addCell(lab);
        lab = new Label(4, 2, record.getAddress());
        sheet.addCell(lab);
        lab = new Label(0, 3, "代收点");
        sheet.addCell(lab);
        lab = new Label(1, 3, record.getCite());
        sheet.addCell(lab);
        lab = new Label(3, 3, "单号");
        sheet.addCell(lab);
        lab = new Label(4, 3, record.getNumber());
        sheet.addCell(lab);


        lab = new Label(0, 5, "取油种类");
        sheet.addCell(lab);
        if(isVeg){
            lab = new Label(1, 5,"菜籽油");
            sheet.addCell(lab);
        }else{
            lab = new Label(1, 5, "精制油");
            sheet.addCell(lab);
        }
        lab = new Label(3, 5, "取油重量");
        sheet.addCell(lab);
        lab = new Label(4, 5, String.valueOf(Weight));
        sheet.addCell(lab);


        double rate, left;
        if(isVeg){
            left = record.getLeftVeg();
            record.setLeftVeg(left-Weight);
            rate = record.getVegRate();
            record.setWeight((int)(record.getWeight()-Weight/rate));
            record.setLeftRefined(record.getWeight()*record.getRefinedRate());
        }else {
            left = record.getLeftRefined();
            record.setLeftRefined(left-Weight);
            rate = record.getRefinedRate();
            record.setWeight((int)(record.getWeight()-Weight/rate));
            record.setLeftVeg(record.getWeight()*record.getVegRate());
        }


        lab = new Label(0, 6, "剩余菜籽重量");
        sheet.addCell(lab);
        lab = new Label(1, 6, String.valueOf(record.getWeight()));
        sheet.addCell(lab);
        lab = new Label(0, 7, "菜籽油调率");
        sheet.addCell(lab);
        lab = new Label(1, 7, String.valueOf(record.getVegRate()));
        sheet.addCell(lab);
        lab = new Label(3, 7, "剩余菜籽油重量");
        sheet.addCell(lab);
        lab = new Label(4, 7, new java.text.DecimalFormat("#.00").format(record.getLeftVeg()));
        sheet.addCell(lab);
        lab = new Label(0, 8, "精制油调率");
        sheet.addCell(lab);
        lab = new Label(1, 8, String.valueOf(record.getRefinedRate()));
        sheet.addCell(lab);
        lab = new Label(3, 8, "剩余精制油重量");
        sheet.addCell(lab);
        lab = new Label(4, 8, new java.text.DecimalFormat("#.00").format(record.getLeftRefined()));
        sheet.addCell(lab);
        workbook.write();
        workbook.close();
    }

    public static void main(String[] args) throws IOException, WriteException {
        RECORD record = new RECORD("王", "jn","1","6002", 68, 0.8, 0.7);
        CreateExcel ce = new CreateExcel();
        ce.fetchOil(record,true, 30);
    }
}

